package com.nnxieli.trans.util

import androidx.appcompat.app.AppCompatActivity
import com.hd.trans.homefun.EventCode
import com.hd.trans.limit4Use.LimitUseListener
import com.hd.trans.utils.LogUtil
import com.hd.trans.utils.PermissionCallback
import com.hd.trans.utils.ToastUtils

/**
 * 限制使用回调实现类：
 * 1. 获取免费试用次数
 * 2. 获取免费试用时长
 * 3. 拦截-跳转收银台
 * 4. 拦截权限事件
 */
class LimitUserImpl: LimitUseListener {

    //单例
    companion object {
        private val instance = LimitUserImpl()
        private var isVip: Boolean = true

        @JvmStatic
        fun setIsVip(isVip: Boolean) {
            this.isVip = isVip
        }

        @JvmStatic
        fun getInstance(): LimitUserImpl {
            return instance
        }
    }

    //获取免费试用次数
    override fun getFreeTrialCount(eventCode: EventCode): Int {
        val freeTrialCount = when (eventCode) {
            EventCode.TEXT_TRANSLATE -> 3
            EventCode.CAMERA_TRANS -> 3
            else -> 0
        }
        return freeTrialCount
    }

    //获取免费试用时长
    override fun getFreeTrialTime(eventCode: EventCode): Long {
        //只有语音翻译和同声传译有试用时长
        val freeTrialTime = when (eventCode) {
            EventCode.VOICE_TRANSLATE -> 1000 * 60 * 1L
            EventCode.SIMUL_TRANSLATE -> 1000 * 60 * 2L
            else -> 0L
        }
        return freeTrialTime
    }

    override fun isAllowTransit(eventCode: EventCode): Boolean {
        return isVip
    }

    override fun intercept(
        eventCode: EventCode,
        title: String?,
        name: String?,
        tab: String?
    ){
        ToastUtils.showNormal("您当前非会员，无法使用该功能")
        //执行拦截处理
        LogUtil.d("LimitUserImpl", "eventName=" + eventCode.eventName + ", title=" + title + ", name=" + name + ", tab=" + tab)
    }

    override fun interceptPermission(
        activity: AppCompatActivity,
        eventCode: EventCode,
        permissions: Array<String>,
        callback: PermissionCallback?
    ): Boolean {
        //执行权限拦截处理
        //false 表示不拦截处理
        return false
    }
}